
import RingSvg from '../components/ring-svg';
import HistogramSvg from '../components/histogram-svg';

export default {
    name: 'collection-statistics',
    components: {
        RingSvg,
        HistogramSvg,
    },
    data() {
        return {
            roleId: '', // 当前用户角色Id
            form: {
                stageName: '',
                teamId: '', // 催收阶段
                collectionUserId: '', // 催收员
            },
            s0: {
                day: {
                    currentDateRepayAmount: '', // 当日回款金额
                    currentDateRecoveryRate: '', // 当日催回率
                    currentDateAssignedAmount: '', // 当日新增分案数
                    currentDateRepayAmountRanking: '', // 当日回款金额排名 1/10
                    currentDateRecoveryRateAmountRanking: '', // 当日催回率排名 1/10
                },
                month: {
                    currentMonthRepayAmount: '', // 当月回款金额
                    currentMonthRecoveryRate: '', // 当月催回率
                    currentMonthAssignedAmount: '', // 当月新增分案数
                    currentMonthRepayAmountRanking: '', // 当月回款金额排名 1/10
                    currentMonthRecoveryRateAmountRanking: '', // 当月催回率排名 1/10
                },
            },
            s123: {
                day: {
                    currentDateAssignedAmount: '', // 当日新增分案数
                    currentDateRepayAmount: '', // 当日回款金额
                    currentDateRepayAmountRanking: '', // 当日回款金额排名 1/10
                    currentDateRepayAmountRankingList: [], // 当日回款金额排序，降序
                    // collectionUserId 催收员id
                    // collectionUserName 催收员姓名
                    // repayAmount 回款金额
                    // rankNo 排序序号
                },
                month: {
                    currentMonthAssignedAmount: '', // 当月新增分案数
                    currentMonthRepayAmount: '', // 当月回款金额
                    currentMonthRepayAmountRanking: '', // 当月回款金额排名 1/10
                    currentMonthRepayAmountRankingList: [], // 当月回款金额排序，降序
                },
            },
            collectorList: [], // 催收员列表
            // 催收阶段
            stageList: [
                { name: 'S0', teamId: '' },
                { name: 'S1', teamId: '' },
                { name: 'S2', teamId: '' },
                { name: 'S3', teamId: '' },
            ],
            // 催收阶段
            stageMap: {
                s0: '',
                s1: '',
                s2: '',
                s3: '',
            },
        };
    },
    methods: {
        // 查询当前用户角色
        getUserRole() {
            this.$api.collection.queryCollectorRole().then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    // 129 主管 128 催收组长 127 催收员 141 预催收员 140 预催收组长 0 待分配
                    if (data.length > 0) {
                        this.roleId = data[0];
                    }
                    if (data.includes('140') || data.includes('128')) {
                        this.queryCollector();
                    }
                    if (data.includes('129')) {
                        this.getCollectStages();
                    }
                    if (data.includes('141') || data.includes('127')) {
                        // 如果是催收员或者预催收员，直接复制用户id
                        this.form.collectionUserId = this.$storage.get('user').id;
                    }
                }
            });
        },
        // 查询催收阶段
        getCollectStages() {
            // { active: 0 }
            this.$api.collection.getCollectStages({ active: 0 }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data && data.length) {
                    data.forEach(item => {
                        this.stageMap[item.name.toLowerCase()] = item.id;
                    });
                    this.stageList.map(item => {
                        item.teamId = this.stageMap[item.name.toLowerCase()];
                    });
                    this.form.stageName = 'S0';
                    this.form.teamId = this.stageMap.s0;
                }
            });
        },
        // 切换不同的都催收阶段
        handleStageNameChange(val) {
            this.form.teamId = this.stageMap[val.toLowerCase()];
            this.resetDataS0Day();
            this.resetDataS0Month();
            this.resetDataS123Day();
            this.resetDataS123Month();
        },
        // 切换不同的催收员
        handleCollectorChange() {
            this.resetDataS0Day();
            this.resetDataS0Month();
            this.resetDataS123Day();
            this.resetDataS123Month();
        },
        // 查询所有管理的催收员
        queryCollector() {
            this.$api.collection.queryCollectorListAll({ roleCode: this.roleId }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.collectorList = data.filter(item => {
                        if (item.isCollect === '1') return item;
                    });
                }
            });
        },
        // 处查询s0当日数据
        leaderS0Day() {
            this.$api.collection.statistics.leaderS0Day().then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s0.day = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS0Day();
                }
            });
        },
        // 查询s0当月数据
        leaderS0Month() {
            this.$api.collection.statistics.leaderS0Month().then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s0.month = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS0Month();
                }
            });
        },
        // s0催收当天
        collectorS0Day() {
            const { form: { collectionUserId } } = this;
            this.$api.collection.statistics.collectorS0Day({ collectionUserId }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s0.day = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS0Day();
                }
            });
        },
        // s0催收当月
        collectorS0Month() {
            const { form: { collectionUserId } } = this;
            this.$api.collection.statistics.collectorS0Month({ collectionUserId }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s0.month = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS0Month();
                }
            });
        },
        // s123查询当日
        s123Day() {
            const { form: { teamId, collectionUserId } } = this;
            this.$api.collection.statistics.s123Day({ teamId, collectionUserId }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s123.day = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS123Day();
                }
            });
        },
        // s123查询当月
        s123Month() {
            const { form: { teamId, collectionUserId } } = this;
            this.$api.collection.statistics.s123Month({ teamId, collectionUserId }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error && data) {
                    this.s123.month = data;
                    this.$message.success(this.$tc('collection.refreshSuccess')); // 刷新成功
                } else {
                    this.$message.error(this.$tc('collection.refreshFail')); // 刷新失败
                    this.resetDataS123Month();
                }
            });
        },
        // 查询当日数据
        queryDayData() {

            if (!this.refreshInterval('day')) return;

            const { roleId, form: { stageName, collectionUserId } } = this;
            // if (['140', '128'].includes(roleId) && !collectionUserId) {
            //     return this.$message.warning(this.$tc('authority.choseCollector')); // 请选择催收员
            // }
            if (('140' === roleId && !collectionUserId) || ('129' === roleId && 'S0' === stageName)) {
                this.leaderS0Day();
            } else if (('140' === roleId && collectionUserId) || '141' === roleId) {
                this.collectorS0Day();
            } else if ((['S1', 'S2', 'S3'].includes(stageName) && '129' === roleId) || '128' === roleId || '127' === roleId) {
                this.s123Day();
            }
        },
        // 查询当月数据
        queryMonthData() {

            if (!this.refreshInterval('month')) return;

            const { roleId, form: { stageName, collectionUserId } } = this;
            // if (['140', '128'].includes(roleId) && !collectionUserId) {
            //     return this.$message.warning(this.$tc('authority.choseCollector')); // 请选择催收员
            // }
            if (('140' === roleId && !collectionUserId) || ('129' === roleId && 'S0' === stageName)) {
                this.leaderS0Month();
            } else if (('140' === roleId && collectionUserId) || '141' === roleId) {
                this.collectorS0Month();
            } else if ((['S1', 'S2', 'S3'].includes(stageName) && '129' === roleId) || '128' === roleId || '127' === roleId) {
                this.s123Month();
            }
        },
        // 重置数据s0 day
        resetDataS0Day() {
            this.s0.day = Object.assign(this.s0.day, {
                currentDateRepayAmount: '', // 当日回款金额
                currentDateRecoveryRate: '', // 当日催回率
                currentDateAssignedAmount: '', // 当日新增分案数
                currentDateRepayAmountRanking: '', // 当日回款金额排名 1/10
                currentDateRecoveryRateAmountRanking: '', // 当日催回率排名 1/10
            });
        },
        // 重置数据s0 month
        resetDataS0Month() {
            this.s0.month = Object.assign(this.s0.day, {
                currentMonthRepayAmount: '', // 当月回款金额
                currentMonthRecoveryRate: '', // 当月催回率
                currentMonthAssignedAmount: '', // 当月新增分案数
                currentMonthRepayAmountRanking: '', // 当月回款金额排名 1/10
                currentMonthRecoveryRateAmountRanking: '', // 当月催回率排名 1/10
            });
        },
        // 重置数据s0 day
        resetDataS123Day() {
            this.s123.day = Object.assign(this.s123.day, {
                currentDateAssignedAmount: '', // 当日新增分案数
                currentDateRepayAmount: '', // 当日回款金额
                currentDateRepayAmountRanking: '', // 当日回款金额排名 1/10
                currentDateRepayAmountRankingList: [], // 当日回款金额排序，降序
            });
        },
        // 重置数据s0 day
        resetDataS123Month() {
            this.s123.month = Object.assign(this.s123.month, {
                currentMonthAssignedAmount: '', // 当月新增分案数
                currentMonthRepayAmount: '', // 当月回款金额
                currentMonthRepayAmountRanking: '', // 当月回款金额排名 1/10
                currentMonthRepayAmountRankingList: [], // 当月回款金额排序，降序
            });
        },
        // 判断刷新间隔是否超过10分钟
        refreshInterval(type) {
            if (['141', '127'].includes(this.roleId)) {
                // 催收员和预催收员增加判断
                const interval = this.$storage.get('interval_' + type);
                if (interval) {
                    if (Date.now() - interval <= 10 * 60 * 1000) {
                        this.$message.warning(this.$tc('collection.refreshIntervalTips'));
                        return false;
                    }
                }
                this.$storage.set('interval_' + type, Date.now());
            }
            return true;
        },
    },
    created() {
        this.getUserRole();
        // this.leaderS0Day();
        // this.leaderS0Month();
    }
}
